package com.travelSpring.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travelSpring.exception.UserException;
import com.travelSpring.mapper.ScenicMapper;
import com.travelSpring.pojo.entity.Scenic;
import com.travelSpring.pojo.entity.User;
import com.travelSpring.pojo.vo.ScenicVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 功能：
 * 作者：卷心菜
 * 日期：2025/4/21 16:38
 */
@Service
public class ScenicService {

    @Autowired
    ScenicMapper scenicMapper;

    public IPage<ScenicVO> selectPage(Integer pageNum, Integer pageSize, Scenic scenic) {
            Page<Scenic> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<Scenic>lqw = new LambdaQueryWrapper<>();
            LocalDate now=LocalDate.now();
            if(scenic!=null){
                if(StringUtils.isNotBlank(scenic.getAddress())){
                    lqw.and(wrapper-> wrapper
                            .apply("start_point->'$[4]' LIKE CONCAT('%', {0}, '%')", scenic.getAddress())
                            .or()
                            .apply("end_point->'$[4]' LIKE CONCAT('%', {0}, '%')", scenic.getAddress())
                    );
                }
                if(scenic.getPrice()!=null){
                    lqw.lt(Scenic::getPrice,scenic.getPrice());
                }
                if(scenic.getTime()!=null){
                    lqw.lt(Scenic::getTime, scenic.getTime());
                }
                if(scenic.getCategoryId()!=null){
                    lqw.eq(Scenic::getCategoryId,scenic.getCategoryId());
                }
                if(scenic.getAutoExtend()!=null){
                    lqw.eq(Scenic::getAutoExtend,scenic.getAutoExtend());
                }
                if(scenic.getStatus()!=null){
                    switch (scenic.getStatus()){
                        case 0:// 已过期
                            lqw.lt(Scenic::getEndTime,now);
                            break;
                        case 1:
                            lqw.le(Scenic::getStartTime,now)
                               .ge(Scenic::getEndTime,now);
                            break;
                        case 2:
                            lqw.gt(Scenic::getEndTime,now);
                            break;
                    }
                }
            }
            lqw.orderByDesc(Scenic::getStartTime);
        return scenicMapper.selectScenicWithPage(page,lqw);
    }

    public void add(Scenic scenic) {
        if(scenic.getStartTime()!=null&&scenic.getEndTime()!=null){
            long daysDiff=ChronoUnit.DAYS.between(
                    scenic.getStartTime(),
                    scenic.getEndTime()
            );
            scenic.setExtendDays((int) daysDiff);
        }
        scenic.setCreateTime(LocalDateTime.now());
        scenicMapper.insert(scenic);
    }

    public void update(Scenic scenic) {
        if(scenic.getStartTime()!=null&&scenic.getEndTime()!=null){
            long daysDiff=ChronoUnit.DAYS.between(
                    scenic.getStartTime(),
                    scenic.getEndTime()
            );
            scenic.setExtendDays((int) daysDiff);
        }
        scenicMapper.updateById(scenic);
    }

    public void delete(Integer id) {
        scenicMapper.deleteById(id);
    }

    public void deleteBatch(List<Scenic> list) {
        List<Integer> ids = list.stream()
                .filter(Objects::nonNull)
                .map(Scenic::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        if(ids.isEmpty()){
            throw new UserException("未提供有效商品ID");
        }
        scenicMapper.deleteBatchIds(ids);
    }

    public void updateBatch(List<Scenic> list) {
        for (Scenic scenic : list) {
            scenicMapper.updateById(scenic);
        }
    }

    public List<Scenic> selectAll(Scenic scenic) {
        LambdaQueryWrapper<Scenic>lqw=new LambdaQueryWrapper<>();
        LocalDate now=LocalDate.now();
        if(scenic!=null&&scenic.getStatus()!=null){
                switch (scenic.getStatus()){
                    case 0:// 已过期
                        lqw.lt(Scenic::getEndTime,now);
                        break;
                    case 1:
                        lqw.le(Scenic::getStartTime,now)
                                .ge(Scenic::getEndTime,now);
                        break;
                    case 2:
                        lqw.gt(Scenic::getEndTime,now);
                        break;
                }
        }else{
            lqw.ge(Scenic::getEndTime,now);
        }
        return scenicMapper.selectList(lqw);
    }

    public Scenic selectById(Integer id) {
        return scenicMapper.selectById(id);
    }


}
